/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package model;

import helper.Utilities;

/**
 *
 * @author kuroshaki
 */
public class Request {
    public Request(){
        this.fileName   = "";
        this.port       = 0;
        this.uploaded   = 0;
        this.left       = 0;
        this.event      = STATUS_EMPTY;
    }

    public Request(Torrent torrent) {
        this.fileName   = torrent.getFileName();
        this.port       = Utilities.TRACKER_PORT;
        this.downloaded = 0;
        this.uploaded   = 0;
        this.left       = 0;
        this.event      = STATUS_STARTED;
    }

    public static Request decodeRequest(String url){
        Request result = new Request();

        System.out.println(url);
        result.fileName     = Utilities.getValueFromHTTPGetURL(url, "filename");
        result.port         = Integer.parseInt(Utilities.getValueFromHTTPGetURL(url, "port"));
        result.downloaded   = Integer.parseInt(Utilities.getValueFromHTTPGetURL(url, "downloaded"));
        result.uploaded     = Integer.parseInt(Utilities.getValueFromHTTPGetURL(url, "uploaded"));
        result.left         = Integer.parseInt(Utilities.getValueFromHTTPGetURL(url, "left"));
        result.event        = Integer.parseInt(Utilities.getValueFromHTTPGetURL(url, "event"));

        return result;
    }

    public String encodeRequest(){
        StringBuilder res = new StringBuilder();

        res.append("filename=");
        res.append(this.fileName);
        res.append("&");
        res.append("port=");
        res.append(this.port);
        res.append("&");
        res.append("downloaded=");
        res.append(this.downloaded);
        res.append("&");
        res.append("uploaded=");
        res.append(this.uploaded);
        res.append("&");
        res.append("left=");
        res.append(this.left);
        res.append("&");
        res.append("event=");
        res.append(this.event);

        return res.toString();
    }

    /** setter */
    public void setFileName(String fileName){
        this.fileName = fileName;
    }

    public void setPort(int p){
        this.port = p;
    }

    public void setDownloaded(int d){
        this.downloaded = d;
    }

    public void setUploaded(int u){
        this.uploaded = u;
    }

    public void setLeft(int l){
        this.left = l;
    }

    public void setEvent(int stat){
        this.event = stat;
    }

    public void setIp(String IP){
        this.ip = IP;
    }

    public void setNumWant(String num){
        this.numWant = num;
    }

    public void setKey(String keys){
        this.key = keys;
    }

    public void setTrackerId(String tracker){
        this.trackerId = tracker;
    }

    /** getter */
    public String getFileName(){
        return this.fileName;
    }

    public int getPort(){
        return this.port;
    }

    public int getDownloaded(){
        return this.downloaded;
    }

    public int getUploaded(){
        return this.uploaded;
    }

    public int getLeft(){
        return this.left;
    }

    public int getEvent(){
        return this.event;
    }

    public String getIp(){
        return this.ip;
    }

    public String getNumWant(){
        return this.numWant;
    }

    public String getKey(){
        return this.key;
    }

    public String getTrackerId(){
        return this.trackerId;
    }

    /** Atribut */
    private String fileName;       //didapat dari info name pada file metainfo
    private int port;               //nomor port sedang listening
    private int downloaded;           //jumlah byte yang telah didownload oleh client
    private int uploaded;           //jumlah byte yang telah diupload oleh client
    private int left;               //jumlah byte yang masih harus didownload
    private int event;           //status apakah file telah terdownload atau belum
    

    /** Tidak dipakai */

    //private String peerId;       //ID dari client (tidak perlu)
    //private int compact;          //bernilai 1 jika menerima respon (tidak perlu)
    //private String noPeerId;    //memberikan pesan ke tracker bahwa dapa tmeniadakan field peer id (tidak perlu)

    /** Optional infos */
    private String ip;              
    private String numWant;
    private String key;
    private String trackerId;

    /** konstanta */
    public static int STATUS_EMPTY      = 0;
    public static int STATUS_STARTED    = 1;
    public static int STATUS_STOPPED    = 2;
    public static int STATUS_COMPLETED  = 3;
}
